<template>
	<view class="container">
		<view class="carousel">
			<swiper indicator-dots circular=true duration="400">
				<swiper-item class="swiper-item" v-for="(item,index) in goodsImgArr" :key="index">
					<view class="image-wrapper">
						<image
							:src="item" 
							class="loaded" 
							mode="aspectFill"
						></image>
					</view>
				</swiper-item>
			</swiper>
		</view>
		
		<view class="introduce-section">
			<text class="title">{{goodsInfo.goodsName}}</text>
			<view class="price-box">
				<text class="price-tip">¥</text>
				<text class="price">{{goodsInfo.goodsPrice}}</text>
				<text class="m-price">¥{{goodsInfo.scribePrice}}</text>
			</view>
			<view class="bot-row">
				<text>销量: {{goodsInfo.totalSales}}</text>
				<text>库存: {{goodsInfo.stockNum}}</text>
				<text>浏览量: {{goodsInfo.goodsViewCount}}</text>
			</view>
		</view>
		
		<view class="c-list">
			<view class="c-row b-b" @click="toggleSpec" v-if="tableData.length>0" style="border-bottom: 1rpx solid #e8e8e8;">
				<text class="tit">购买类型</text>
				<view class="con">
					<text class="selected-text" v-for="(sItem, sIndex) in specSelected" :key="sIndex">
						{{sItem}}
					</text>
				</view>
				<view class="">
					<uv-icon name="arrow-right" color="#999" size="16"></uv-icon>
				</view>
			</view>
			<view class="c-row b-b" @click="topath">
				<text class="tit">优惠券</text>
				<text class="con t-r red">领取优惠券</text>
				<view class="">
					<uv-icon name="arrow-right" color="#999" size="16"></uv-icon>
				</view>
			</view>
			
		</view>
		
		
		<view class="detail-desc">
			<view class="d-header">
				<text>图文详情</text>
			</view>
			<uv-parse :content="goodsInfo.goodsDetailsJson"></uv-parse>
		</view>
		
		<!-- 底部操作菜单 -->
		<view class="page-bottom">
			<navigator url="/pages/index/index" open-type="switchTab" class="p-b-btn">
				<view class="">
					<uv-icon name="home" color="#999" size="26"></uv-icon>
				</view>
				<text>首页</text>
			</navigator>
			<navigator url="/pages/cart/cart" open-type="switchTab" class="p-b-btn">
				<view class="">
					<uv-icon name="shopping-cart" color="#999" size="26"></uv-icon>
				</view>
				<text>购物车</text>
			</navigator>
			<view class="p-b-btn" :class="{active: favorite}" @click="toFavorite">
				<view class="">
					<uv-icon name="heart-fill" :color="favorite?'#ff633d':'#999'" size="26"></uv-icon>
				</view>
				
				<text>收藏</text>
			</view>
			
			<view class="action-btn-group">
				<button type="primary" class=" action-btn no-border buy-now-btn" @click="buy">立即购买</button>
				<button type="primary" class=" action-btn no-border add-cart-btn" @click="joinCart()">加入购物车</button>
			</view>
		</view>
		
		
		<!-- 规格-模态层弹窗 -->
		<view 
			class="popup spec" 
			:class="specClass"
			@touchmove.stop.prevent="stopPrevent"
			@click="toggleSpec"
		>
			<!-- 遮罩层 -->
			<view class="mask"></view>
			<view class="layer attr-content" @click.stop="stopPrevent">
				<view class="a-t">
					<image :src="sku.specificationImages"></image>
					<view class="right">
						<text class="price">¥{{sku.dealPrice}}</text>
						<text class="stock">库存：{{sku.stockNum}}件</text>
						<view class="selected">
							已选：
							<text class="selected-text" v-for="(sItem, sIndex) in specSelected" :key="sIndex">
								{{sItem}}
							</text>
						</view>
					</view>
				</view>
				<view v-for="(item,index) in specList" :key="index" class="attr-list">
					<text>{{item.name}}</text>
					<view class="item-list">
						<text 
							v-for="(childItem, childIndex) in item.children" 
							:key="childIndex" class="tit"
							:class="{selected:childItem.selected}"
							@click="selectSpec(index,childIndex, item)"
						>
							{{childItem.attributeValue}}
						</text>
					</view>
				</view>
				<button class="btn" @click="toggleSpec">完成</button>
			</view>
		</view>
		
	</view>
</template>

<script>
	import { insertPlugsShopCart,searchPlugsGoodsById,searchPlugsGoodsSpecificationByGoodsId,insertPlugsCollect,searchPlugsCollectByUserId } from '../../utils/api.js';
	export default{
		data() {
			return {
				specClass: 'none',
				specSelected:[],
				
				favorite: false,
				shareList: [],
				imgList: [
					{
						src: 'https://gd3.alicdn.com/imgextra/i3/0/O1CN01IiyFQI1UGShoFKt1O_!!0-item_pic.jpg_400x400.jpg'
					},
					{
						src: 'https://gd3.alicdn.com/imgextra/i3/TB1RPFPPFXXXXcNXpXXXXXXXXXX_!!0-item_pic.jpg_400x400.jpg'
					},
					{
						src: 'https://gd2.alicdn.com/imgextra/i2/38832490/O1CN01IYq7gu1UGShvbEFnd_!!38832490.jpg_400x400.jpg'
					}
				],
				desc: `
					
				`,
				specList: [
					
				],
				
			
				goodsId:0,
				goodsInfo:{},
				goodsImgArr:[],
				sku:{},
				tableData:[],
				nowBusinessInfo:{}
			};
		},
		async onLoad(options){
			
			//接收传值,id里面放的是标题，因为测试数据并没写id 
			let id = options.id;
			if(id){
				// this.$api.msg(`点击了${id}`);
				this.goodsId=id
			}
			
			
			
		},
		onShow() {
			this.nowBusinessInfo = uni.getStorageSync('nowBusinessInfo');
			this.specSelected=[]
			this.getGoodsList()
			this.getguige()
			this.getcollectList()
		},
		methods:{
			topath(){
				
					uni.navigateTo({
						url:'/pages/index/coupon/coupon'
					})
			},
			getGoodsList(){
				let data={
					id:this.goodsId
				}
				searchPlugsGoodsById(data).then(res=>{
					if(res.code==200){
						this.goodsInfo=res.body
						let list = [];
						if (res.body.rotationImgsJson != '') {
							list = JSON.parse(res.body.rotationImgsJson)
						} else {
							list[0] = res.body.previewUrl;
						}
						this.goodsImgArr = list;
						uni.setNavigationBarTitle({
							title: res.body.goodsName
						});
						
					}
				})
			},
			getcollectList() {
				let self = this;
				let postdata = {
					businessId: this.nowBusinessInfo.jsonViewData.businessId.toString(),
					page: 1,
					size: 50
				}
				searchPlugsCollectByUserId(postdata).then(res => {
					if (res.code == 200) {
						let list2 = res.body.list;
						console.log(list2,99996);
						let data = list2.filter(itm => itm.dataId == this.goodsId);
						console.log(data,966);
						if (data.length > 0) {
							self.favorite = true;
						}
					}
				})
			},
			getguige(){
				let data = {
					goodsId: this.goodsId
				};
				searchPlugsGoodsSpecificationByGoodsId(data).then(res => {
					if (res.code == 200) {
						if (res.body.specificationInfo.length !== 0) {
							let arr = [];
							// this.specificationInfo = res.body.specificationInfo;
							for (let item of res.body.specificationInfo) {
								let obj1 = {
									name: Object.keys(item)[0],
									value: [],
									title: '',
									children: Object.values(item)[0]
								};
								console.log(item, 456);
								arr.push(obj1);
							}
							let skunameList=[]
							arr.forEach(item => {
								console.log(item.children, 7777);
								item.children[0].selected=true
								this.specSelected.push(item.children[0].attributeValue);
								let name=`${item.name}:${item.children[0].attributeValue}`
								skunameList.push(name)
								
							});
							let skuname=JSON.stringify(skunameList).replace(/\"/g, '').replace(/\[|]/g, '')
							console.log(skuname);
							this.specList = arr;
							console.log(this.specList,'数组');
							let arr1 = [];
							res.body.plugsGoodsSpecificationList.forEach(item => {
									item.specificationName= item.specificationName.replace(/\"/g, '').replace(/\{|}/g, '')
							});
							this.tableData = res.body.plugsGoodsSpecificationList;
							console.log(this.tableData );
							this.sku = this.tableData.find(item => item.specificationName === skuname)
							console.log(this.sku);
						}
					}
				});
			},
			//规格弹窗开关
			toggleSpec() {
				if(this.specClass === 'show'){
					this.specClass = 'hide';
					setTimeout(() => {
						this.specClass = 'none';
					}, 250);
				}else if(this.specClass === 'none'){
					this.specClass = 'show';
				}
			},
			//选择规格
			selectSpec(index, itemindedx,item){
				item.children.forEach((element)=>{
					element.selected=false
				})
				item.children[itemindedx].selected= true
				this.$set(item.children[itemindedx], 'selected', true);
				this.specSelected = [];
				let skunameList=[]
				this.specList.forEach(items=>{
					items.children.forEach((res=>{
						
						if(res.selected === true){
							console.log(res.attributeValue);
							this.specSelected.push(res.attributeValue); 
							let name=`${items.name}:${res.attributeValue}`
							skunameList.push(name)
						} 
					}))
					
				})
				let skuname=JSON.stringify(skunameList).replace(/\"/g, '').replace(/\[|]/g, '')
				
				
				// console.log(skuname);
				// skuname='红色大s'
				// let arr = ['大小:大,颜色:红色,尺码:s', '颜色:红色,大小:大,尺码:m', '三'];
				//     //false
				//     console.log(arr.includes(skuname));
				//     console.log(arr[0]);
				this.sku = this.tableData.find(item => item.specificationName === skuname)
				
			},
			//分享
			share(){
				this.$refs.share.toggleMask();	
			},
			//收藏
			toFavorite(){
				
				let postData={
					"businessId": this.nowBusinessInfo.jsonViewData.businessId.toString(),
					"dataId": this.goodsId,
					"types": 0,
				}
				let token = uni.getStorageSync('userInfo').accessToken;
					if (token == null) {
						uni.showModal({
							title: '提示',
							content: '当前未登录',
							success: function(res) {
								if (res.confirm) {
									uni.navigateTo({
										url: '/pages/login/login'
									});
								} else if (res.cancel) {
								}
							}
						});
					} else {
						insertPlugsCollect(postData).then(res => {
							// console.log("insertPlugsShopCart", res)
							if (res.code == 200) {
								this.favorite = !this.favorite;
								if(this.favorite){
									uni.showToast({
										title: '收藏成功',
										duration: 1000,
										icon: 'success'
									});
								}else{
									uni.showToast({
										title: '取消收藏',
										duration: 1000,
										icon: 'none'
									});
								}
							} else {
								
							}
						});
					}
			},
			buy(){
				console.log(JSON.stringify(this.sku));
				let postData = {
					goodsId: this.goodsInfo.id,
					previewUrl:this.goodsInfo.previewUrl,
					goodsPrice:this.goodsInfo.goodsPrice,
					goodsCategoryId: this.goodsInfo.categoryId,
					goodsNum: 1,
					goodsSpecificationIds: this.sku.id||0,
					goodsSpecification: JSON.stringify(this.sku)=='{}'?[]: [this.sku],
					businessId: this.nowBusinessInfo.jsonViewData.businessId.toString(),
					distributionFee: this.goodsInfo.distributionFee,
					goodsName: this.goodsInfo.goodsName,
					plugsGoods:this.goodsInfo,
				};
				
				console.log(postData);
				if (this.tableData.length!=0) {
					if (this.sku.stockNum == 0) {
						uni.showModal({
							title: '提示',
							content: '库存不足!',
							showCancel: false
						});
						return
					}
				}else{
					if (this.goodsInfo.stockNum == 0) {
						uni.showModal({
							title: '提示',
							content: '库存不足!',
							showCancel: false
						});
						return
					}
				}
				uni.navigateTo({
					url: `/pages/order/createOrder?data=${encodeURIComponent(JSON.stringify([postData]))}`
				});
				
				
			},
			joinCart (){
				let token = uni.getStorageSync('userInfo').accessToken;
				console.log(token);
				if (token == null) {
					uni.showModal({
						title: '提示',
						content: '登录才能添加宝贝哦~',
						success: function(res) {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/login/login'
								});
							} else if (res.cancel) {
							}
						}
					});
				} else {
					let postData = {
						goodsId: this.goodsInfo.id,
						goodsCategoryId: this.goodsInfo.categoryId,
						goodsNum: 1,
						goodsSpecificationIds: this.sku.id||0,
						businessId: this.nowBusinessInfo.jsonViewData.businessId.toString()
					};
					if (this.tableData.length!=0) {
						if (this.sku.stockNum == 0) {
							uni.showModal({
								title: '提示',
								content: '库存不足!',
								showCancel: false
							});
							return
						}
					}else{
						if (this.goodsInfo.stockNum == 0) {
							uni.showModal({
								title: '提示',
								content: '库存不足!',
								showCancel: false
							});
							return
						}
					}
					insertPlugsShopCart(postData).then(res => {
						// console.log("insertPlugsShopCart", res)
						if (res.code == 200) {
							uni.showToast({
								title: '加入成功',
								duration: 1000,
								icon: 'success'
							});
						} else {
							uni.showToast({
								title: '加入失败',
								duration: 1000,
								icon: 'error'
							});
						}
					});
				}
			},
			stopPrevent(){}
		},

	}
</script>

<style lang='scss'>
	page{
		background: #f8f8f8;
		padding-bottom: 160rpx;
	}
	.icon-you{
		font-size: 30rpx;
		color: #888;
	}
	.carousel {
		height: 722rpx;
		position:relative;
		swiper{
			height: 100%;
		}
		.image-wrapper{
			width: 100%;
			height: 100%;
		}
		.swiper-item {
			display: flex;
			justify-content: center;
			align-content: center;
			height: 750rpx;
			overflow: hidden;
			image {
				width: 100%;
				height: 100%;
			}
		}
		
	}
	
	/* 标题简介 */
	.introduce-section{
		background: #fff;
		padding: 20rpx 30rpx;
		
		.title{
			font-size: 32rpx;
			color: #303133;
			height: 50rpx;
			line-height: 50rpx;
		}
		.price-box{
			display:flex;
			align-items:baseline;
			height: 64rpx;
			padding: 10rpx 0;
			font-size: 26rpx;
			color:#ff633d;
		}
		.price{
			font-size:34rpx;
		}
		.m-price{
			margin:0 12rpx;
			color: #909399;
			text-decoration: line-through;
		}
		.coupon-tip{
			align-items: center;
			padding: 4rpx 10rpx;
			background: #ff633d;
			font-size: 24rpx;
			color: #fff;
			border-radius: 6rpx;
			line-height: 1;
			transform: translateY(-4rpx); 
		}
		.bot-row{
			display:flex;
			align-items:center;
			height: 50rpx;
			font-size: 24rpx;
			color: #909399;
			text{
				flex: 1;
			}
		}
	}
	/* 分享 */
	.share-section{
		display:flex;
		align-items:center;
		color: #606266;
		background: linear-gradient(left, #fdf5f6, #fbebf6);
		padding: 12rpx 30rpx;
		.share-icon{
			display:flex;
			align-items:center;
			width: 70rpx;
			height: 30rpx;
			line-height: 1;
			border: 1px solid #ff633d;
			border-radius: 4rpx;
			position:relative;
			overflow: hidden;
			font-size: 22rpx;
			color: #ff633d;
			&:after{
				content: '';
				width: 50rpx;
				height: 50rpx;
				border-radius: 50%;
				left: -20rpx;
				top: -12rpx;
				position:absolute;
				background: #ff633d;
			}
		}
		.icon-xingxing{
			position:relative;
			z-index: 1;
			font-size: 24rpx;
			margin-left: 2rpx;
			margin-right: 10rpx;
			color: #fff;
			line-height: 1;
		}
		.tit{
			font-size: 28rpx;
			margin-left:10rpx;
		}
		.icon-bangzhu1{
			padding: 10rpx;
			font-size: 30rpx;
			line-height: 1;
		}
		.share-btn{
			flex: 1;
			text-align:right;
			font-size: 24rpx;
			color: #ff633d;
		}
		.icon-you{
			font-size: 24rpx;
			margin-left: 4rpx;
			color: #ff633d;
		}
	}
	
	.c-list{
		font-size: 24rpx + 2rpx;
		color: #606266;
		background: #fff;
		.c-row{
			display:flex;
			align-items:center;
			padding: 20rpx 30rpx;
			position:relative;
		}
		.tit{
			width: 140rpx;
		}
		.con{
			flex: 1;
			color: #303133;
			.selected-text{
				margin-right: 10rpx;
			}
		}
		.bz-list{
			height: 40rpx;
			font-size: 24rpx+2rpx;
			color: #303133;
			text{
				display: inline-block;
				margin-right: 30rpx;
			}
		}
		.con-list{
			flex: 1;
			display:flex;
			flex-direction: column;
			color: #303133;
			line-height: 40rpx;
		}
		.red{
			color: #ff633d;
		}
	}
	
	/* 评价 */
	.eva-section{
		display: flex;
		flex-direction: column;
		padding: 20rpx 30rpx;
		background: #fff;
		margin-top: 16rpx;
		.e-header{
			display: flex;
			align-items: center;
			height: 70rpx;
			font-size: 24rpx + 2rpx;
			color: #909399;
			.tit{
				font-size: 28rpx + 2rpx;
				color: #303133;
				margin-right: 4rpx;
			}
			.tip{
				flex: 1;
				text-align: right;
			}
			.icon-you{
				margin-left: 10rpx;
			}
		}
	}
	.eva-box{
		display: flex;
		padding: 20rpx 0;
		.portrait{
			flex-shrink: 0;
			width: 80rpx;
			height: 80rpx;
			border-radius: 100px;
		}
		.right{
			flex: 1;
			display: flex;
			flex-direction: column;
			font-size: 28rpx;
			color: #606266;
			padding-left: 26rpx;
			.con{
				font-size: 28rpx;
				color: #303133;
				padding: 20rpx 0;
			}
			.bot{
				display: flex;
				justify-content: space-between;
				font-size: 24rpx;
				color:#909399;
			}
		}
	}
	/*  详情 */
	.detail-desc{
		background: #fff;
		margin-top: 16rpx;
		.d-header{
			display: flex;
			justify-content: center;
			align-items: center;
			height: 80rpx;
			font-size: 30rpx;
			color: #303133;
			position: relative;
				
			text{
				padding: 0 20rpx;
				background: #fff;
				position: relative;
				z-index: 1;
			}
			&:after{
				position: absolute;
				left: 50%;
				top: 50%;
				transform: translateX(-50%);
				width: 300rpx;
				height: 0;
				content: '';
				border-bottom: 1px solid #ccc; 
			}
		}
	}
	
	/* 规格选择弹窗 */
	.attr-content{
		padding: 10rpx 30rpx;
		.a-t{
			display: flex;
			image{
				width: 170rpx;
				height: 170rpx;
				flex-shrink: 0;
				margin-top: -40rpx;
				border-radius: 8rpx;;
			}
			.right{
				display: flex;
				flex-direction: column;
				padding-left: 24rpx;
				font-size: 24rpx + 2rpx;
				color: #606266;
				line-height: 42rpx;
				.price{
					font-size: 32rpx;
					color: #ff633d;
					margin-bottom: 10rpx;
				}
				.selected-text{
					margin-right: 10rpx;
				}
			}
		}
		.attr-list{
			display: flex;
			flex-direction: column;
			font-size: 28rpx + 2rpx;
			color: #606266;
			padding-top: 30rpx;
			padding-left: 10rpx;
		}
		.item-list{
			padding: 20rpx 0 0;
			display: flex;
			flex-wrap: wrap;
			text{
				display: flex;
				align-items: center;
				justify-content: center;
				background: #eee;
				margin-right: 20rpx;
				margin-bottom: 20rpx;
				border-radius: 100rpx;
				min-width: 60rpx;
				height: 60rpx;
				padding: 0 20rpx;
				font-size: 28rpx;
				color: #303133;
			}
			.selected{
				background: #fbebee;
				color: #ff633d;
			}
		}
	}
	
	/*  弹出层 */
	.popup {
		position: fixed;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		z-index: 99;
		
		&.show {
			display: block;
			.mask{
				animation: showPopup 0.2s linear both;
			}
			.layer {
				animation: showLayer 0.2s linear both;
			}
		}
		&.hide {
			.mask{
				animation: hidePopup 0.2s linear both;
			}
			.layer {
				animation: hideLayer 0.2s linear both;
			}
		}
		&.none {
			display: none;
		}
		.mask{
			position: fixed;
			top: 0;
			width: 100%;
			height: 100%;
			z-index: 1;
			background-color: rgba(0, 0, 0, 0.4);
		}
		.layer {
			position: fixed;
			z-index: 99;
			bottom: 0;
			width: 92%;
			min-height: 40vh;
			border-radius: 10rpx 10rpx 0 0;
			background-color: #fff;
			.btn{
				height: 66rpx;
				line-height: 66rpx;
				border-radius: 100rpx;
				background: #ff633d;
				font-size: 28rpx + 2rpx;
				color: #fff;
				margin: 30rpx auto 20rpx;
			}
		}
		@keyframes showPopup {
			0% {
				opacity: 0;
			}
			100% {
				opacity: 1;
			}
		}
		@keyframes hidePopup {
			0% {
				opacity: 1;
			}
			100% {
				opacity: 0;
			}
		}
		@keyframes showLayer {
			0% {
				transform: translateY(120%);
			}
			100% {
				transform: translateY(0%);
			}
		}
		@keyframes hideLayer {
			0% {
				transform: translateY(0);
			}
			100% {
				transform: translateY(120%);
			}
		}
	}
	
	/* 底部操作菜单 */
	.page-bottom{
		position:fixed;
		left: 30rpx;
		bottom:30rpx;
		z-index: 95;
		display: flex;
		justify-content: center;
		align-items: center;
		width: 690rpx;
		height: 100rpx;
		background: rgba(255,255,255,.9);
		box-shadow: 0 0 20rpx 0 rgba(0,0,0,.5);
		border-radius: 16rpx;
		
		.p-b-btn{
			display:flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			font-size: 24rpx;
			color: #606266;
			width: 96rpx;
			height: 80rpx;
			.yticon{
				font-size: 40rpx;
				line-height: 48rpx;
				color: #909399;
			}
			&.active, &.active .yticon{
				color: #ff633d;
			}
			.icon-fenxiang2{
				font-size: 42rpx;
				transform: translateY(-2rpx);
			}
			.icon-shoucang{
				font-size: 46rpx;
			}
		}
		.action-btn-group{
			display: flex;
			height: 76rpx;
			border-radius: 100px;
			overflow: hidden;
			box-shadow: 0 20rpx 40rpx -16rpx  #ff633d;
			box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
			background: linear-gradient(to right, #ffac30, #ff633d,#F56C6C);
			margin-left: 20rpx;
			position:relative;
			&:after{
				content: '';
				position:absolute;
				top: 50%;
				right: 50%;
				transform: translateY(-50%);
				height: 28rpx;
				width: 0;
				border-right: 1px solid rgba(255,255,255,.5);
			}
			.action-btn{
				display:flex;
				align-items: center;
				justify-content: center;
				width: 180rpx;
				height: 100%;
				font-size: 28rpx ;
				padding: 0;
				border-radius: 0;
				background: transparent;
			}
		}
	}
	
</style>
